package com.cuit.bysj.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.cuit.bysj.dao.AppraiseDao;
import com.cuit.bysj.entities.Appraise;
import com.cuit.bysj.entities.TcherResults;
import com.cuit.bysj.entities.User;
import com.cuit.bysj.entities.adminResults;

public class AppraiseService {
	private AppraiseDao appraiseDao;

	public AppraiseDao getAppraiseDao() {
		return appraiseDao;
	}

	public void setAppraiseDao(AppraiseDao appraiseDao) {
		this.appraiseDao = appraiseDao;
	}

	public Appraise findAppraise(Integer id) {
		return appraiseDao.getByID(id);
	}

	// 学生用户查询评教表
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List queryListbyId(User user) {
		List<Appraise> list = new ArrayList<Appraise>();

		if (user.getStu() != null) {
			list = appraiseDao.loadlist("from Appraise a where a.student=" + user.getStu().getId() + " and a.score<>0");
		}

		list = sortList(list);

		return list;
	}

	public List<Appraise> findListbyTcherId(String id, String content, String banji) {
		String hql = "from Appraise a where a.teacher = " + "'" + id + "'"
				+ " and a.score > 0 and a.content <> null and a.subject.sName=" + "'" + content + "'"
				+ "and a.student.banji=" + banji;

		List<Appraise> list = appraiseDao.loadlist(hql);

		return list;
	}

	// 教师查询根据班级评教表
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List queryListbyInfo(String str1, String str2, String str3) {
		String hql = "from Appraise a where a.student.major=" + "'" + str1 + "'" + " and a.student.banji=" + "'" + str2
				+ "'" + " and a.subject.sName=" + "'" + str3 + "'" + " and a.score<>0 and a.content <> null";

		List<Appraise> list = appraiseDao.loadlist(hql);

		list = sortList(list);

		return list;
	}

	// 生成管理员查看的教师列表
	@SuppressWarnings("rawtypes")
	public List<adminResults> initAdminSeeList() {
		String hql = "select a.teacher.id,a.teacher.tname,a.subject.sName,a.student.major,a.student.banji,avg(a.score)"
				+ " from Appraise a "
				+ "group by a.student.major,a.student.banji,a.teacher.id,a.subject.id having avg(a.score)<>0";

		List list = appraiseDao.loadlist(hql);
		List<adminResults> results = new ArrayList<adminResults>();
		for (int i = 0; i < list.size(); i++) {
			Object[] obj = (Object[]) list.get(i);
			adminResults a = new adminResults((Integer) obj[0], String.valueOf(obj[1]), String.valueOf(obj[2]),
					String.valueOf(obj[3]), String.valueOf(obj[4]), String.valueOf(obj[5]));
			results.add(a);
		}

		return results;
	}

	public List<Appraise> sortList(List<Appraise> list) {
		MyComparator mc = new MyComparator();
		Collections.sort(list, mc);
		System.out.println("排序后的值");
		for (int i = 0; i < list.size(); i++) {
			Appraise emp = (Appraise) list.get(i);
			System.out.println(emp.getScore());
		}

		return list;
	}

	// 内部类实现实现Comparator接口的compare方法
	class MyComparator implements Comparator<Appraise> {
		public int compare(Appraise obj1, Appraise obj2) {
			Appraise a1 = (Appraise) obj1;
			Appraise a2 = (Appraise) obj2;
			if (a1.getScore() < a2.getScore()) {
				return -1;
			} else if (a1.getScore() == a2.getScore()) {
				return 0;
			} else
				return 1;
		}
	}

	public void updateAppraise(Appraise appraise) {
		appraiseDao.update(appraise);
	}

	// 根据老师查询老师所教班级列表
	public List<TcherResults> initTcherSeelist(String id) {
		String hql = "select a.student.major,a.student.banji,a.subject.sName,avg(a.score) from Appraise a where a.teacher="
				+ id + "group by a.student.major,a.student.banji";

		List list = appraiseDao.loadlist(hql);
		List<TcherResults> results = new ArrayList<TcherResults>();
		for (int i = 0; i < list.size(); i++) {
			Object[] obj = (Object[]) list.get(i);
			TcherResults a = new TcherResults(String.valueOf(obj[0]), String.valueOf(obj[1]), String.valueOf(obj[2]),
					String.valueOf(obj[3]));
			results.add(a);
		}
		return results;
	}
}
